def crc16_cal(datalist):

    test_crc=0xffff
    poly=0xa001

    numl = len(datalist)
    for num in range(numl):
        data = datalist[num]
        test_crc=(data&0xff)^test_crc

        for bit in range(8):

            if(test_crc&0x1) != 0:

                test_crc>>=1
                test_crc^=poly

            else:

                test_crc>>=1

    crc_val = format(test_crc, '04x')

    crc_val = "0x" + crc_val

    return crc_val

def pack_data(data: bytearray) ->bytearray: 

    '''
    data: 要效验的数据

    return: 效验完的数据， 末尾添加了crc值

    data = bytearray([0x01, 0x06, 0x00, 0x12, 0x00, 0x01])

    return  bytearray(b'\x01\x06\x00\x12\x00\x01\xe8\x0f')
    
    '''
    print(crc16_cal(data))
    crc_val = crc16_cal(data)[2:]

    print(crc_val)

    first= int("0x" + crc_val[:2], 16)
    second= int("0x" + crc_val[2:], 16)

    data.append(first)
    data.append(second)

    return data

x = bytearray(b'\x06\x03\x03\xb7\x01^@\xdc\xf8\x00 R\x1a\x02*\xa9\xd8\xde\xf8\x00 \x12\x01\x10\xd4\xdc\xf8\x00 R\x1a\x02*\xa0\xd8\xde\xf8\x00 \x12\x01\x07\xd4\xdc\xf8\x00 R\x1a\x02*O\xf0\x03\x02\xd9\xd9\x06\xe0\x80i\xde\xf8\x10\x10\x00"\x08C\xce\xf8')

command = pack_data(x)

print(command)